package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.corba.se.spi.orb.StringPair;

import ex.com.jm.food.entity.CompanyProfit;
import ex.com.jm.food.entity.FoodCart;
import ex.com.jm.food.entity.Order;
import ex.com.jm.food.entity.OrderItem;
import ex.com.jm.food.entity.User;
import ex.com.jm.food.service.UserOrderService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;
import ex.com.jm.food.utils.RandomUtils;

/**
 * Action层
 * @author zhaojw
 *
 */
public class UserOrderAction extends ActionSupport {

	
	private static final long serialVersionUID = 1L;
	
    private Order order;
    private OrderItem orderItem;
    private UserOrderService userOrderService;
    private int initPage=1;
    private int pageRow=10;
    private int totalCount;
    private String userPhone;
    private String s_date;
    private String e_date;
    private String cIds;
    private String userId;
    private String comId;
    private String orderId;
    private String street;
    private String address;
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	public OrderItem getOrderItem() {
		return orderItem;
	}
	public void setOrderItem(OrderItem orderItem) {
		this.orderItem = orderItem;
	}
	public UserOrderService getUserOrderService() {
		return userOrderService;
	}
	public void setUserOrderService(UserOrderService userOrderService) {
		this.userOrderService = userOrderService;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getUserPhone() {
		return userPhone;
	}
	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public String getcIds() {
		return cIds;
	}
	public void setcIds(String cIds) {
		this.cIds = cIds;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	/**
	 * 列表页面
	 * @return
	 */
	public String orderList(){
		String phone=(String) ActionContext.getContext().getSession().get("phone");
		List<Order> orders=this.getUserOrderService().getOrders("187011187857", s_date, e_date, 0, 1);
		System.out.println(orders.size());
		if(orders!=null){
			order=orders.get(0);
		}
		return "orderList";
		
	}
	/**
	 * 获取订单数据
	 */
	public void getOrderList(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<Order> orders=this.getUserOrderService().getOrders(userPhone, s_date, e_date, offset, pageRow);
		JSONArray ja=JSONArray.fromObject(orders);
		totalCount=this.getUserOrderService().orderCount(userPhone, s_date, e_date);
		JSONObject jo=JsonUtils.buildJson(ja, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	
	public String saveOrder(){
		ActionContext context = ActionContext.getContext();
		Map param = context.getParameters();
		Set<OrderItem> orderItems = new HashSet<OrderItem>();
	//	Order order = new Order();
		//订单数据
		System.out.println(comId+"<<<<===>>>"+userId);
		order.setComId(this.getUserOrderService().getCompany(comId));
		order.setUserId(this.getUserOrderService().getUser(userId));
		order.setUserAddress(street+";"+address);
		order.setPayStatus(1);
		order.setOrderFlage(0);
		order.setOrderDate(DateUtils.getCurrentDate());
		order.setOrderId(RandomUtils.createId());
		order.setUserName(this.getUserOrderService().getUser(userId).getRealName());
		ActionContext.getContext().getSession().put("phone", order.getUserPhone());
		//订单详情数据
		String[] cid=(String[]) param.get("cartId");
		String[] num=(String[]) param.get("num");
		
		for (int i = 0; i < cid.length; i++) {
			int proNum = Integer.parseInt(num[i]);
			//double mp=Double.parseDouble(profit[i]);
			FoodCart foodCart=this.getUserOrderService().getFoodCart(cid[i]);
			OrderItem orderItem=new OrderItem();
			orderItem.setMenuId(foodCart.getMenuId());
			orderItem.setMenuName(foodCart.getMenuName());
			orderItem.setMenuNowPrice(foodCart.getMenuPrice());
			orderItem.setMenuNum(proNum);
			orderItem.setProfit(proNum*foodCart.getMenuPrice());
			orderItems.add(orderItem);
			this.getUserOrderService().saveOrderItem(orderItem);
			CompanyProfit companyProfit=new CompanyProfit();
			companyProfit.setComId(comId);
			companyProfit.setCpDate(DateUtils.getCurrentDate());
			companyProfit.setMenuId(foodCart.getMenuId());
			companyProfit.setMenuNum(proNum);
			companyProfit.setMenuPrice(foodCart.getMenuPrice());
			companyProfit.setMenuProfit(proNum*foodCart.getMenuPrice());
			this.getUserOrderService().saveCompanyProfit(companyProfit);
		}
		order.setOrderItems(orderItems);
		this.getUserOrderService().saveOrder(order);
		//汇总表数据
		
		return "list";
	}
	
	/**
	 * 取消订单
	 * @return
	 */
	public String delOrder(){
		this.getUserOrderService().delOrderItem(orderId);
		this.getUserOrderService().delOrder(orderId);
		return "list";
	}
	
	public void getOrderItemList(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<OrderItem> orderItems=this.getUserOrderService().getOrderItems(orderId, offset, pageRow);
		JSONArray ja=JSONArray.fromObject(orderItems);
		totalCount=this.getUserOrderService().orderItemCount(orderId);
		JSONObject jo=JsonUtils.buildJson(ja, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	
	
}
